/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.opu.vcforms.template;

import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;

/**
 *
 * @author yura
 */
@Root(name="data")
public class Data implements Iterable<Data> {
    private static final String TYPE_VAR = "var";
    private static final String TYPE_TXT = "txt";
    private static final String TYPE_CONTENT = "content";

//    @Attribute(required = false)
    private String type = TYPE_TXT;

//    @ElementMap(entry = "property", key = "name",
//            attribute = true, required = false, data = true,
//            keyType = String.class, valueType = String.class)
    private Properties properties;

//    @Text(data = true, required = false)
//    @Element(data = true, required = false, name="text")
    private String text;

    @ElementList(inline = true, required = false, type = Data.class)
    private List<Data> childrens;

    private Delegator delegator = new TextDelegator();

    public List<Data> getChildrens() {
        if (TYPE_TXT.equals(type)){
            childrens = null;
        }

        return childrens;
    }

    public void setChildrens(List<Data> childrens) {
        this.childrens = childrens;
    }

    @Attribute(required = false)
    public String getType() {
        return type;
    }

    @Attribute(required = false)
    public void setType(String type) {
        Validator.assertIsOneOf("Can't init data type", type,
                TYPE_CONTENT, TYPE_TXT, TYPE_VAR);

        this.type = type;

        if (TYPE_TXT.equals(type)){
            delegator = new TextDelegator();
        } else if (TYPE_VAR.equals(type)){
            delegator = new VarDelegator();
        } else if (TYPE_CONTENT.equals(type)){
            delegator = new ContentDelegator();
        } else {
            throw new IllegalArgumentException();
        }

        if (properties != null){
            setProperties(properties);
        }

        if (text != null){
            delegator.setText(text);
        }
    }

    @ElementMap(entry = "property", key = "name",
            attribute = true, required = false, data = true,
            keyType = String.class, valueType = String.class)
    public Properties getProperties() {
        return properties;
    }

    @ElementMap(entry = "property", key = "name",
            attribute = true, required = false, data = true,
            keyType = String.class, valueType = String.class)
    public void setProperties(Properties properties) {
        this.properties = properties;
        for (String name : properties.stringPropertyNames()) {
            delegator.setProperty(name, properties.getProperty(name));
        }
    }

    @Element(data = true, required = false, name="text")
    public String getText() {
        return text;
    }

    @Element(data = true, required = false, name="text")
    public void setText(String text) {
        this.text = text;

        if (delegator != null){
            delegator.setText(text);
        }
    }

    public Delegator getDelegator() {
        return delegator;
    }

    public boolean isVar(){
        return TYPE_VAR.equals(type);
    }
    
    public boolean isContent(){
        return TYPE_CONTENT.equals(type);
    }
    
    public boolean isText(){
        return TYPE_TXT.equals(type);
    }

    @Override
    public Iterator<Data> iterator() {
        return childrens.iterator();
    }
}
